<?php

namespace App\Models;

use App\Engine\BaseModel;
use App\Exceptions\ApiException;
use App\Engine\TokenService;

class Admin extends BaseModel
{
    protected $fillable=['username','password','login_time','login_ip'];

    public static function login($data)
    {
        $count=self::count();
        $data['password']=md5($data['password']);
        if($count==0){
            self::create($data);
        }
        $res=self::where($data)->first();
        if(!$res)
            throw new ApiException('账号或密码错误');
        $cache=[
            'aid'=>$res->id
        ];
        return ['token'=>(new TokenService())->saveCache($cache)];



    }

}
